package com.example.mask.maskmobilesshop.controller;


import com.example.mask.maskmobilesshop.entity.Address;
import com.example.mask.maskmobilesshop.entity.Member;
import com.example.mask.maskmobilesshop.entity.vo.AddressRequest;
import com.example.mask.maskmobilesshop.enums.HttpStatus;
import com.example.mask.maskmobilesshop.service.AddressService;
import com.example.mask.maskmobilesshop.service.IMemberService;
import com.example.mask.maskmobilesshop.utils.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址的Controller
 */
@RestController
@RequestMapping(value = "/address")
public class AddressController {


    private Logger logger = LoggerFactory.getLogger(LoginController.class.getName());

    @Autowired
    private AddressService addressService;

    @Autowired
    private IMemberService memberService;


    @ApiOperation(value = "获取地址列表接口", notes = "返回: 2000-成功 1000-会员不存在 3000-参数传入不正确 5000-服务器异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/getAddressList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HashMap<String, Object> getAddressList(String mobile) {

        HashMap<String, Object> result = new HashMap<>();
        try {
            if (!StringUtils.isEmpty(mobile)) {
                List<Address> addressList = addressService.findByMemberId(Long.parseLong(mobile));
                result.put("address", addressList);
                result.put("status", HttpStatus.SUCCESS.getValue());
            }
            result.put("status", HttpStatus.BAD_PARAMETERS.getValue());
        } catch (Exception e) {
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("获取属性列表发生了异常，错误异常如下:");
            e.printStackTrace();

        }
        return result;
    }

    @ApiOperation(value = "修改或者新增地址接口", notes = "返回: 2000-成功 1000-会员不存在 3000-参数传入不正确 5000-服务器异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "addressRequest", value = "用户手机号码", required = true, dataType = "String", paramType = "body")})
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HashMap<String, Object> saveOrUpdate(@RequestBody AddressRequest addressRequest) {
        return null;
    }


    /**
     * 设置默认的信息
     *
     * @param addressRequest
     * @return
     */
    @ApiOperation(value = "设置默认地址信息", notes = "返回: 2000-成功 1000-会员不存在 3000-参数传入不正确 5000-服务器异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "addressRequest", value = "用户手机号码", required = true, dataType = "String", paramType = "body")})
    @RequestMapping(value = "/setDefaultAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HashMap<String, Object> setDefaultAddress(@RequestBody AddressRequest addressRequest) {

        HashMap<String, Object> result = new HashMap<>();
        try {
            if (!StringUtils.isEmpty(addressRequest.getMobile())) {
                Member member = memberService.findByName(addressRequest.getMobile());
                if (member != null) {
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put("isDefault", addressRequest.getIsDefault());
                    params.put("id", addressRequest.getId());
                    params.put("type", 1);

                    addressService.updateDefaultAddress(params);

                    params.put("isDefault", 0);
                    params.put("type", 0);
                    params.put("memberId", member.getMemberId());
                    addressService.updateDefaultAddress(params);
                    result.put("status", HttpStatus.SUCCESS.getValue());
                } else {
                    result.put("status", HttpStatus.MEMBER_NOT_EXIST.getValue());
                }

            }
            result.put("status", HttpStatus.BAD_PARAMETERS.getValue());
        } catch (Exception e) {
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("设置默认地址发生了异常，错误异常如下:");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 刪除地址信息
     *
     * @param ids
     * @param mobile
     * @return 是否成功
     */
    @ApiOperation(value = "删除地址信息", notes = "返回: 2000-成功 1000-会员不存在 3000-参数传入不正确 5000-服务器异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "删除的地址ID", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, dataType = "String", paramType = "body")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String, Object> delete(String ids, String mobile) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            if (!StringUtils.isEmpty(mobile)) {
                Member member = memberService.findByName(mobile);
                if (member != null) {
                    String[] idss = ids.split(",");
                    List<String> idList = Arrays.asList(idss);
                    boolean isDelete = addressService.deleteBatchIds(idList);
                    if (isDelete) {
                        result.put("status", HttpStatus.SUCCESS.getValue());
                    }
                }
                result.put("status", HttpStatus.MEMBER_NOT_EXIST.getValue());
            }
            result.put("status", HttpStatus.BAD_PARAMETERS.getValue());
        } catch (Exception e) {
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("刪除地址发生了异常，错误异常如下:");
            e.printStackTrace();

        }
        return result;
    }
}
